package com.kh.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kh.controller.HelloController.testE;
import com.kh.pojo.HelloPojo;
import com.kh.pojo.MenuPoJo;
import com.kh.service.MenuService;
import com.kh.service.SayHello;
import com.kh.utils.Tools;

@Controller
@RequestMapping("/hello")
public class HelloController {
	@Autowired
	SayHello sayHello;
	
	@Autowired
	MenuService menuService;
	
//	http://localhost:1234/learn/hello.do?test
	@RequestMapping(params="test")
//	@ResponseBody
	public String test(){
		return "kit_admin/index";
	}

//	http://localhost:1234/learn/Hello.do?sayHello
	@RequestMapping(params="sayHello")
	@ResponseBody
	public List<HelloPojo> sayHello(HttpServletRequest servletRequest) {

//		Bean bean = new Bean();
//		bean.name = "张三";
		HelloPojo pojo=new HelloPojo();
		pojo.setContent("%内%");
		List<HelloPojo> data = sayHello.getData(pojo);
		System.out.println(data);
		return data;
	}

	@RequestMapping("/success")
//	@ResponseBody
	public String success(HttpServletRequest request) {

//		Bean bean = new Bean();
//		bean.name = "张三";
		HelloPojo pojo=new HelloPojo();
		pojo.setContent("%内%");
		List<HelloPojo> data = sayHello.getData(pojo);
		System.out.println(data);
		request.setAttribute("context", request.getContextPath());
		return "hello/success";
	}

//	http://localhost:1234/learn/hello.do?save
	@RequestMapping(params="save")
	public String save() throws testE {
		HelloPojo hello = new HelloPojo();
		hello.setId(Tools.getRandom());
		hello.setTitle("标题");
		hello.setContent("内容");
//		sayHello.save(hello);
		throw new testE();
//		return "hello/success";
	}
	static class testE extends Exception{
		
	}
//	http://localhost:1234/learn/hello.do?saveMS
	@RequestMapping(params="saveMS")
	public String saveMS() {
		HelloPojo hello = new HelloPojo();
		hello.setId(Tools.getRandom());
		hello.setTitle("标题ms");
		hello.setContent("内容ms");
		sayHello.saveMs(hello);
		return "hello/success";
	}

	@RequestMapping("/errorSave")
	public String errorSave() {
		HelloPojo hello = new HelloPojo();
		hello.setId(Tools.getRandom());
		hello.setTitle("标题");
		hello.setContent("内容");
		sayHello.save(hello);
		return "hello/success";
	}

//	public static class Bean {
//		public String name;
//	}

}
